package com.learn.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.shop.pojo.entriy.TaskHistory;
import com.learn.shop.mapper.TaskHistoryMapper;
import com.learn.shop.pojo.vo.PageVo;
import com.learn.shop.service.TaskHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 历史定时任务(TaskHistory)表服务实现类
 *
 * @author zhengdechang
 * @since 2022-01-04 11:28:48
 */
@Service
public class TaskHistoryServiceImpl implements TaskHistoryService {
    @Autowired
    private TaskHistoryMapper taskHistoryMapper;


    /**
     * 分页
     *
     * @param pageVo
     * @return
     */
    @Override
    public PageInfo<TaskHistory> list(PageVo pageVo) {

        PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<TaskHistory> list = taskHistoryMapper.list();
        list.forEach(dto -> {
            String operation = dto.getOperation();
            if ("1".equals(operation)) {
                dto.setOperation("开启");
            } else {
                dto.setOperation("关闭");
            }
        });
        return new PageInfo<>(list);

    }

    @Override
    public TaskHistory newOperation() {
        return taskHistoryMapper.selectNewOperation();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TaskHistory queryById(Integer id) {
        return this.taskHistoryMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param taskHistory 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<TaskHistory> queryByPage(TaskHistory taskHistory, PageRequest pageRequest) {
        long total = this.taskHistoryMapper.count(taskHistory);
        return new PageImpl<>(this.taskHistoryMapper.queryAllByLimit(taskHistory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param taskHistory 实例对象
     * @return 实例对象
     */
    @Override
    public TaskHistory insert(TaskHistory taskHistory) {
        this.taskHistoryMapper.insert(taskHistory);
        return taskHistory;
    }

    /**
     * 修改数据
     *
     * @param taskHistory 实例对象
     * @return 实例对象
     */
    @Override
    public TaskHistory update(TaskHistory taskHistory) {
        this.taskHistoryMapper.update(taskHistory);
        return this.queryById(taskHistory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.taskHistoryMapper.deleteById(id) > 0;
    }
}
